﻿// Example header text. Can be configured in the options.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using vtech_frameWorks.DAO;
using vtech_frameWorks.Exceptions;
using vtech_frameWorks.Object;

namespace PharmacySystem.Unit
{
    class ClsUnitDAO : ClsObjectDAO
    {
        public override void Delete(System.Data.SqlClient.SqlTransaction sqlTransaction, System.Data.DataRow dataRow)
        {
            try
            {
                SqlParameter[] paramArray = new SqlParameter[]
                {
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@ID", SqlDbType.UniqueIdentifier, 1, ParameterDirection.Input),
                };
                ClsBaseDbAccessDAO.SetParameterValues(paramArray, dataRow);

                ClsBaseDbAccessDAO.ExecuteNonQuery(sqlTransaction, "DELETE FROM [tblUnit] WHERE [ID] = @ID", paramArray);
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.Message);
            }
        }

        public override void Edit(System.Data.SqlClient.SqlTransaction sqlTran, System.Data.DataRow dRow)
        {
            try
            {
                SqlParameter[] paramArray = new SqlParameter[]
                {
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@ID", SqlDbType.UniqueIdentifier, 0, ParameterDirection.Input),
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@Code", SqlDbType.NVarChar, 50, ParameterDirection.Input),
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@Name", SqlDbType.NVarChar, 50, ParameterDirection.Input),
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@Description", SqlDbType.NVarChar, 255, ParameterDirection.Input),
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@Inactive", SqlDbType.Bit, 1, ParameterDirection.Input),
                };
                ClsBaseDbAccessDAO.SetParameterValues(paramArray, dRow);
                ClsBaseDbAccessDAO.ExecuteProcedure(sqlTran, "spEditUnit", paramArray);
            }
            catch (Exception ex)
            {
                ClsInformation.Information(ex.Message);
                return;
            }
        }

        public override void Insert(System.Data.SqlClient.SqlTransaction sqlTran, System.Data.DataRow dRow)
        {
            try
            {
                SqlParameter[] paramArray = new SqlParameter[]
                {
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@Code", SqlDbType.NVarChar, 50, ParameterDirection.Input),
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@Name", SqlDbType.NVarChar, 50, ParameterDirection.Input),
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@Description", SqlDbType.NVarChar, 255, ParameterDirection.Input),
                    ClsBaseDbAccessDAO.CreateSqlPrameter("@Inactive", SqlDbType.Bit, 1, ParameterDirection.Input),
                };
                ClsBaseDbAccessDAO.SetParameterValues(paramArray, dRow);
                ClsBaseDbAccessDAO.ExecuteProcedure(sqlTran, "spAddUnit", paramArray);
            }
            catch (Exception ex)
            {
                ClsInformation.Information(ex.Message);
                return;
            }
        }

        public override System.Data.DataTable GetAll(System.Data.SqlClient.SqlTransaction sqlTransaction)
        {
            try
            {
                return ClsBaseDbAccessDAO.ExecuteProcedure(sqlTransaction, "spGetUnit").Tables[0];
            }
            catch (Exception ex)
            {
                ClsInformation.Error(ex.Message);
                return null;
            }
        }
    }
}